import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:waterfall_flow/waterfall_flow.dart';
import 'package:xvideos/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:xvideos/hj_utils/api_service/common_service.dart';
import 'package:xvideos/hj_utils/api_service/vid_service.dart';
import 'package:xvideos/routers/jump_router.dart';
import 'package:xvideos/tools_base/debug_log.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

import '../../../../hj_model/video_model.dart';
import '../../../alert_business/video/recommend_video_res.dart';
import '../../../hj_model/home/home_tabs_entity.dart';

class ShortFollowView extends StatefulWidget {
  final ModuleData? model;

  ShortFollowView({this.model});

  @override
  State<StatefulWidget> createState() {
    return _ShortFollowViewState();
  }
}

class _ShortFollowViewState extends State<ShortFollowView> {
  List<VideoModel>? dataArr;
  int currentPage = 1;
  RefreshController? refreshController;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _loadData();
    });
  }

  void _loadData({int page = 1, int size = 10}) async {
    try {
      RecommendListRes? retModel = await CommonService.fetchMyFollowDynamicList(page: page, size: size, type: 1, newsType: 'SHORT');
      currentPage = page;
      dataArr ??= [];
      if (page == 1) {
        dataArr?.clear();
      }
      dataArr?.addAll(retModel?.vInfo ?? []);
      if (retModel?.hasNext == true) {
        refreshController?.loadComplete();
      } else {
        refreshController?.loadNoData();
      }
    } catch (e) {
      dataArr ??= [];
      refreshController?.loadComplete();
      debugLog(e);
    }
    refreshController?.refreshCompleted();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (dataArr == null) {
      return LoadingCenterWidget();
    } else if (dataArr!.isEmpty) {
      return CErrorWidget(
        retryOnTap: () {
          dataArr = null;
          setState(() {});
          _loadData();
        },
      );
    } else {
      return pullYsRefresh(
        onLoading: (refreshController) {
          _loadData(page: currentPage + 1);
        },
        onRefresh: (refreshController) {
          _loadData();
        },
        onInit: (ctr) => refreshController = ctr,
        child: CustomScrollView(
          slivers: [
            SliverPadding(
              padding: const EdgeInsets.fromLTRB(12, 0, 12, 0),
              sliver: SliverWaterfallFlow(
                gridDelegate: const SliverWaterfallFlowDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  crossAxisSpacing: 6,
                  mainAxisSpacing: 10,
                ),
                delegate: SliverChildBuilderDelegate(
                  (BuildContext c, int index) {
                    VideoModel videoModel = dataArr![index];
                    var ratio = 168 / 266;
                    return VideoSimpleCellWidget(
                      videoModel: videoModel,
                      imgAspectRatio: ratio,
                      textLines: 1,
                      onTap: () {
                        if (videoModel.isRandomAd() == true) {
                          pushToPageByLink(videoModel.randomAdsInfo?.href, id: videoModel.randomAdsInfo?.id);
                        } else {
                          pushToVideoPage(videoModel: videoModel, videoArr: dataArr);
                        }
                      },
                    );
                  },
                  childCount: dataArr?.length ?? 0,
                ),
              ),
            ),
          ],
        ),
      );
    }
  }
}
